package com.udi.project.business.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.udi.common.utils.security.ShiroUtils;
import com.udi.framework.aspectj.lang.annotation.Log;
import com.udi.framework.aspectj.lang.enums.BusinessType;
import com.udi.framework.web.controller.BaseController;
import com.udi.framework.web.domain.AjaxResult;
import com.udi.framework.web.page.TableDataInfo;
import com.udi.project.business.base.domain.BaseCorpVo;
import com.udi.project.business.base.service.BaseCorpService;
import com.udi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/base")
public class Base_Controller extends BaseController {

    private String pre = "base";

    @Autowired
    BaseCorpService baseCorpService;

    /**
     * 企业基本信息视图
     * @return
     */
    @RequiresPermissions("base:corp:view")
    @GetMapping("/corp")
    public String base(){
        return pre+"/corp/main";
    }

    /**
     * 企业注册备案关系信息视图
     * @return
     */
    @RequiresPermissions("base:zhuce:view")
    @GetMapping("/zhuce")
    public String base_(){
        return pre+"/zhuce/main";
    }

    /**
     *企业基本信息详细信息视图
     * @param
     * @return java.lang.String
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/11 17:10
     */

    @GetMapping("/detail_corp")
    public String base_0(){
        return pre+"/corp/detail";
    }
    /**
     *企业基本信息添加视图
     * @param
     * @return java.lang.String
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/11 17:10
     */
    @GetMapping("/add_corp")
    public String base_1(){
        return pre+"/corp/add";
    }
    /**
     *企业基本信息编辑视图
     * @param
     * @return java.lang.String
     * @author yuxin
     * @creed: Talk is cheap,show me the code
     * @date 2021/10/11 17:10
     */
    @RequestMapping("/edit_corp/{Id}")
    public String corp_edit(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("BaseCorpVo", baseCorpService.selectCorpById(id));
        return pre + "/corp/edit";
    }

    @GetMapping("/add_zcba")
    public String base_5(){
        return pre+"/zhuce/add";
    }

    @GetMapping("/edit_zcba/{Id}")
    public String edit_zcba(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("BaseCorpVo", baseCorpService.selectZcbaById(id));
        return pre + "/zhuce/edit";
    }

    @GetMapping("/detail_zcba/{Id}")
    public String base_4(@PathVariable("Id") Integer id, ModelMap mmap){
        mmap.put("BaseCorpVo", baseCorpService.selectZcbaById(id));
        return pre+"/zhuce/detail";
    }


    @RequiresPermissions("base:corp:list")
    @RequestMapping("/corp_list")
    @ResponseBody
    public TableDataInfo data(BaseCorpVo name){
        //开启分页，待数据查询时进行sql组装
        startPage();
        //获取当前用户数据
        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");
        //获取权限标识符，查询得出是否包含字符“admin”
        String roleKey =((JSONObject)((JSONArray)user_arr.get("roles")).get(0)).getString("roleKey");
        List<BaseCorpVo> list;
        //根据普通用户和管理员查询不同权限数据
        if(roleKey.contains("admin")){
            name.setDeptId("0");
            list = baseCorpService.selectcorplist(name);
        }else{
            name.setDeptId(current_deptId);
            list = baseCorpService.selectcorplist(name);
        }
        return getDataTable(list);
    }

    @Log(title = "企业信息维护", businessType = BusinessType.INSERT)
    @RequestMapping("/corp_add")
    @ResponseBody
    public AjaxResult corp_add(BaseCorpVo baseCorpVo){
        try{
            return toAjax(baseCorpService.insert_corp(baseCorpVo));
        }catch (Exception e){
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    @Log(title = "企业信息维护", businessType = BusinessType.DELETE)
    @RequestMapping("/corp_edit")
    @ResponseBody
    public AjaxResult corp_edit(BaseCorpVo baseCorpVo) {
        try {
            return toAjax(baseCorpService.updateCorp(baseCorpVo));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    @Log(title = "企业信息维护", businessType = BusinessType.DELETE)
    @RequestMapping("/corp_del")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(baseCorpService.del_corp(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @RequestMapping("/corp_detail")
    @ResponseBody
    public AjaxResult corp_detail(Integer id){
        System.out.println("base:corp:del------------"+id);
        return AjaxResult.error();
    }


    @RequiresPermissions("base:zhuce:list")
    @RequestMapping("/zcba_list")
    @ResponseBody
    public TableDataInfo data_admin(BaseCorpVo baseCorpVo){
        //String s = "[{\"a\":\"916101315784432974\",\"b\":\"外科器械\",\"c\":\"李xx\",\"d\":\"2023-09-02\"}]";

        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");
        String roleKey =((JSONObject)((JSONArray)user_arr.get("roles")).get(0)).getString("roleKey");
        List<BaseCorpVo> list;
        //角色权限数据筛选
        if(roleKey.contains("admin")){
            baseCorpVo.setDeptId("0");
            list = baseCorpService.selectzcbarlist(baseCorpVo);
        }else {
            baseCorpVo.setDeptId(current_deptId);
            list = baseCorpService.selectzcbarlist(baseCorpVo);
        }

        return getDataTable(list);
    }
    @Log(title = "注册备案信息维护", businessType = BusinessType.INSERT)
    @RequiresPermissions("base:zhuce:add")
    @RequestMapping("/zcba_add")
    @ResponseBody
    public AjaxResult data_add(BaseCorpVo baseCorpVo){
        try{
            return toAjax(baseCorpService.insert_zcba(baseCorpVo));
        }catch (Exception e){
            System.out.println(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }


    @Log(title = "注册备案信息维护", businessType = BusinessType.UPDATE)
    @RequiresPermissions("base:zhuce:edit")
    @RequestMapping("/zcba_edit")
    @ResponseBody
    public AjaxResult data_edit(BaseCorpVo baseCorpVo){
        try {
            return toAjax(baseCorpService.updateZcba(baseCorpVo));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    @Log(title = "注册备案信息维护", businessType = BusinessType.DELETE)
    @RequiresPermissions("base:zhuce:del")
    @RequestMapping("/zcba_del")
    @ResponseBody
    public AjaxResult data_del(String ids){
        System.out.println("base:zhuce:del---------------->"+ids);
        try {
            return toAjax(baseCorpService.del_zcba(ids));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }



    /**
     * 校验企业名称是否存在
     * @return
     */
    @PostMapping("/checkDeptNameUnique")
    @ResponseBody
    public String checkDeptNameUnique(BaseCorpVo baseCorpVo)
    {
        return baseCorpService.checkDeptNameUnique(baseCorpVo);
    }
    /**
     * 校验企业统一社会信用代码是否存在
     * @return
     */
    @PostMapping("/checkTyshxydmNameUnique")
    @ResponseBody
    public String checkTyshxydmNameUnique(BaseCorpVo baseCorpVo)
    {
        return baseCorpService.checkTyshxydmUnique(baseCorpVo);
    }
    /**
     * 校验厂商码是否存在
     * @return
     */
    @PostMapping("/checkCorpCodeUnique")
    @ResponseBody
    public String checkCorpCodeUnique(BaseCorpVo baseCorpVo)
    {
        return baseCorpService.checkCorpCodeUnique(baseCorpVo);
    }

    /**
     * 校验注册备案号是否存在
     * @return
     */
    @PostMapping("/checkZcbahUnique")
    @ResponseBody
    public String checkZcbahUnique(BaseCorpVo baseCorpVo) {
        return baseCorpService.checkZcbahUnique(baseCorpVo);
    }
    /**
     * 校验注册备案产品名称是否存在
     * @return
     */
    @PostMapping("/checkZcbacpmcUnique")
    @ResponseBody
    public String checkZcbacpmcUnique(BaseCorpVo baseCorpVo)
    {
        return baseCorpService.checkZcbacpmcUnique(baseCorpVo);
    }

    //辅助信息
    @RequestMapping("/getdept")
    @ResponseBody
    public JSONArray getdept(){
        //获取当前用户数据
        JSONObject user_arr = JSONObject.parseObject(JSONObject.toJSON(ShiroUtils.getSysUser()).toString());
        String current_deptId = ((JSONObject)user_arr.get("dept")).getString("deptId");
        //获取权限标识符，查询得出是否包含字符“admin”
        String roleKey =((JSONObject)((JSONArray)user_arr.get("roles")).get(0)).getString("roleKey");
        List<BaseCorpVo> list;
        if(roleKey.contains("admin")){
           list = baseCorpService.getdept("0");
        }else{
           list = baseCorpService.getdept(current_deptId);
        }
        return JSONArray.parseArray(JSON.toJSONString(list));
    }
}
